package com.ltmall.api.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class RemoteUtil {
    private StringBuffer errmsg = new StringBuffer();

    /**
     * 请求class封装
     */
    @SuppressWarnings("rawtypes")
    Class clazz = null;

    @SuppressWarnings("rawtypes")
    public RemoteUtil(Class clazz1) {
        clazz = clazz1;
    }

    public static Object request(HttpServletRequest request, Object object) {
        String reqJson = request.getParameter("requestParam");
        // String reqJson = "";
        RequestParam req = null;
        // IOS
        if (reqJson == null) {
            // Android.
            try {
                BeanUtils.populate(object, request.getParameterMap());
            } catch (Exception e) {
                // this.errmsg.append(e.getMessage()+ "\n");
                e.printStackTrace();
                return null;
            }
        }
        return object;
    }

    public static void response(HttpServletResponse response, Object object,
                                int code, String context) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jo = new JSONObject();
        jo.put("data", object);
        jo.put("code", code);
        jo.put("context", context);
        out.print(jo);
        out.flush();
        out.close();
    }

    public static void responseTwo(HttpServletResponse response, Object object,
                                   Object object2, int code, String context) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jo = new JSONObject();
        jo.put("data1", object);
        jo.put("data2", object2);
        jo.put("code", code);
        jo.put("context", context);
        out.print(jo);
        out.flush();
        out.close();
    }

    public static void responseSingleJson(HttpServletResponse response,
                                          Object object) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jo = new JSONObject();
        jo.put("bussiness", object);
        // out.print(object);
        out.print(jo);
        out.flush();
        out.close();
    }

    public static void responseJson(HttpServletResponse response,
                                    Object object) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jo = new JSONObject();
        jo.put("json", object);
        out.print(jo);
        out.flush();
        out.close();
    }

}
